package com.mak.menoy.local;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.mak.menoy.entiy.UserInfo;

public class UserInfoCache {
	private static final String SETTING_NAME = "AIZANQIAN";
	private SharedPreferences share;
	private UserInfo mUserInfo;
	public static final String FIRST_STARTUP_BOOLEAN_FLAG = "start_up";
	public static final String MUSTGUID203="mustguid203";
    public static final String LINKID="linkid";
	public UserInfoCache(Context c) {
		share = c.getSharedPreferences(SETTING_NAME,
				Activity.MODE_WORLD_READABLE);
		mUserInfo = new UserInfo();
	}

	public boolean isFirstStartUp() {
		int firstStartUp = share.getInt("isFirstStartUp", -1);
		if (firstStartUp == -1 || firstStartUp == 1) {
			share.edit().putInt("isFirstStartUp", 0).commit();
			return true;
		} else
			return false;
	}

	public void put(String key, String value) {
		Editor editor = share.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public void put(String key, boolean value) {
		Editor editor = share.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public String getString(String key) {
		return share.getString(key, null);
	}

	public boolean getTrueBoolean(String key) {
		return share.getBoolean(key, true);
	}

	public UserInfo get() {
		if (share != null ) {
			String username = share.getString("username", null);
			String password = share.getString("password", null);
			String zfbAccout = share.getString("zfbaccout", null);
			String qqAccout = share.getString("qqaccout", null);
			String mobileAccout = share.getString("mobileaccout", null);
			String credits = share.getString("credits", null);
			String pendscre = share.getString("pendscre", null);
			if (username != null)
				mUserInfo.setAccout(username);
			if (password != null)
				mUserInfo.setPwd(password);
			if (zfbAccout != null)
				mUserInfo.setZfbAccout(zfbAccout);
			if (qqAccout != null)
				mUserInfo.setQqAcout(qqAccout);
			if (mobileAccout != null) {
				mUserInfo.setMobileAccout(mobileAccout);
			}
			if (credits!=null) {
				mUserInfo.setCredits(credits);
			}
			if (pendscre!=null) {
				mUserInfo.setPendcredits(pendscre);
			}

		}
		return mUserInfo;
	}

	public boolean isLogin() {
		if (share != null ) {
			String username = share.getString("username", null);
			if (username == null ) {
				return false;
			}
			return true;
		}
		return false;

	}

	public void saveToLocal(UserInfo userInfo) {
		save(userInfo);
	}

	public void save(UserInfo userInfo) {
		if (userInfo == null)
			throw new NullPointerException("user info can not be null");
		// clearUserInfo();
		Editor editor = share.edit();
		editor.putString("username", userInfo.getAccout());
		editor.putString("password", userInfo.getPwd());
		editor.putString("zfbaccout", userInfo.getZfbAccout());
		editor.putString("qqaccout", userInfo.getQqAcout());
		editor.putString("mobileaccout", userInfo.getMobileAccout());
		editor.putString("credits", userInfo.getCredits());
		editor.putString("pendscre", userInfo.getPendcredits());
		editor.commit();
	}

	public void clearUp() {
		UserInfo info = new UserInfo();
		mUserInfo = info;
		save(info);
	}
}
